﻿

//显示
$(function () {
    Show()
    WarehouseShow()
})
function Show() {
    layui.use(function () {
        var table = layui.table;
        var reservoirName = $("#reservoirName").val()
        var reservoirNumber = $("#reservoirNumbers").val();
        // 先进行表格的初始渲染
        table.render({
            elem: '#carriertable',  // 表格元素的选择器
            page: {
                limit: 5,
                limits: [5, 8, 10]
            },
            cols: [[
                { type: 'checkbox', fixed: 'left' },
                { field: 'reservoirName', title: '库区名称' },
                { field: 'reservoirNumber', title: '库区编号' },
                { field: 'reservoirArea', title: '面积' },
                { field: 'warehouseID', title: '所属仓库' },
                //{ field: 'reservoirRemark', title: '库位数量' },
                { field: 'reservoirRemark', title: '备注' },
                {
                    field: '', title: '操作', templet: function (d) {
                        return '<a class="layui-btn layui-btn-edit layui-btn-xs" onclick="editData(' + d.reservoirId + ')"><i class="layui-icon layui-icon-edit"></i>编辑</a> ' +
                            '<a class="layui-btn layui-btn-danger layui-btn-xs" onclick="deleteData(' + d.reservoirId + ')"><i class="layui-icon layui-icon-delete"></i>删除</a>';
                    }
                }
            ]],
            // 初始时可以先不加载数据，等待 ajax 请求成功后再更新
            data: []
        });
        var data = {
            pageIndex: 1,
            pageSize: 80,
            reservoirName: reservoirName,
            reservoirNumber: reservoirNumber,
            Token: localStorage.getItem('token')
        }
        console.log(data)
        $.ajax({
      
            url: "/Warehouses/ReservoirShowsj",
            type: "get",
            data: data,
            success: function (data) {
                data = JSON.parse(data);
                console.log(data.data.plist);
                console.log(data.data)
                // 在 ajax 请求成功后，使用正确的表格 ID 进行 reload
                table.reload('carriertable', {
                    page: {
                        limit: 5,
                        limits: [5, 8, 10]
                    },
                    data: data.data.plist
                });
            },
            error: function () {
                console.log("请求失败");
            }
        });
    })
}

//添加
layui.use('layer', function () {
    var $ = layui.$;
    var layer = layui.layer;
    var util = layui.util;
    var form = layui.form;
    var table = layui.table;
    // 事件
    util.on('lay-on', {
        'test-page-custom': function () {
            layer.open({
                type: 1,
                title: '库区管理添加',
                content: `
        <form class="layui-form" action="">
          <div class="layui-form-item">
            <div class="layui-inline">
              <label class="layui-form-label">库区名称:</label>
              <div class="layui-input-inline">
                    <input type="text" name="reservoirName"lay-verify="required"  id="reservoirName" autocomplete="off" class="layui-input">
              </div>
            </div>
            <div class="layui-inline">
              <label class="layui-form-label">库区编号:</label>
              <div class="layui-input-block">
                <input type="text" name="reservoirNumber" lay-verify="required" id="reservoirNumber" autocomplete="off" class="layui-input">
              </div>
            </div>
            <div class="layui-inline">
              <label class="layui-form-label">面积:</label>
              <div class="layui-input-inline">
                    <input type="text" name="reservoirArea"lay-verify="required"  id="reservoirArea" autocomplete="off" class="layui-input">
              </div>
            </div>
            <div class="layui-inline">
              <label class="layui-form-label">所属仓库:</label>
              <div class="layui-col-md6">
                <select id="warehouseID" name="warehouseID" lay-verify="required" lay-filter="mySelectFilter" class="dialog-select">
                      <option value="">请选择</option>
                      <option value="仓库一">仓库一</option>
                      <option value="仓库二">仓库二</option>
                      <option value="仓库三">仓库三</option>
                </select>
              </div>
            </div>
          </div>
          <div class="layui-form-item layui-form-text">
            <label class="layui-form-label">备注</label>
            <div class="layui-input-block">
                      <textarea placeholder="请输入内容" lay-verify="required" name="reservoirRemark" id="reservoirRemark" class="layui-textarea"></textarea>
            </div>
          </div>
          <div class="layui-form-item" style="text-align:center">
                <button class="layui-btn" lay-submit lay-filter="demo-login">确认</button>
            <button type="reset" class="layui-btn layui-btn-primary">取消</button>
          </div>
        </form>`,
                area: ['700px', '350px'],
                success: function () {
                    WarehouseShow()
                    // 对弹层中的表单进行初始化渲染
                    form.render();
                    // 表单提交事件
                    form.on('submit(demo-login)', function (data) {

                        var field = data.field;

                        $.ajax({
                            url: '/Warehouses/ReservoirAdd',
                            type: 'post',
                            data: { data: JSON.stringify(field), token: localStorage.getItem("token") },
                            success: function (res) {
                                var s = JSON.parse(res)
                                if (s.code == 1) {
                                    layer.alert("添加成功");
                                    setTimeout(function () {
                                        location.reload();
                                    }, 2000);
                                }
                                else {
                                    layer.alert("添加失败");
                                }
                            }
                        })
                        return false; // 阻止默认 form 跳转
                    });
                }
            });

            // 获取当前日期时间
            var now = new Date();
            var hours = ("0" + now.getHours()).slice(-2);
            var minutes = ("0" + now.getMinutes()).slice(-2);
            var seconds = ("0" + now.getSeconds()).slice(-2);
            var serialNumber = "KQ"+ hours + minutes + seconds;
            var inputElement = document.getElementById('reservoirNumber');
            inputElement.value = serialNumber;
            console.log(inputElement.value)
        }
    })
});
//所属仓库
function WarehouseShow() {
    layui.use('form', function () {
        var form = layui.form;
        $.ajax({
            url: "/Warehouses/LedgerInfowarehouseShow",
            type: "get",
            data: {
                pageindex: 1,
                pagesize: 50,
            },
            success: function (data) {
                var obj = JSON.parse(data);
                console.log(obj.data.dtoList)
                var options = '<option value="">请选择</option>'; // 添加默认选项
                $.each(obj.data.dtoList, function (index, item) {
                    options += '<option value="' + item.warehouseName + '">' + item.warehouseName + '</option>';
                });
                $(".dialog-select#warehouseID").html(options);
                form.render('select'); // 重新渲染下拉框
            },
            error: function () {
                console.log("获取数据失败");
            }
        });
    });
}

//编辑
var obj = {
    reservoirId: '',
    reservoirName: '',
    reservoirNumber: '',
    reservoirArea: '',
    warehouseID: '',
    reservoirRemark: '',
    addName: '',
    addTime: ''
}
window.editData = function (id) {
    // 先根据id获取要编辑的数据，发起Ajax请求
    var form = layui.form;
    //反填库区
    $.ajax({
        type: "get",
        url: "/Warehouses/GetReservoirId",  // 替换为实际的后台根据id获取数据的接口地址
        data: { Id: id, token: localStorage.getItem("token") },
        success: function (res) {
            var res = JSON.parse(res)
            console.log(res.data)
            $(res.data).each(function () {
                $('#reservoirId').val(this.reservoirId)
                $('#reservoirNames').val(this.reservoirName)
                $('#reservoirNumber').val(this.reservoirNumber)
                $('#reservoirArea').val(this.reservoirArea)
                $('#warehouseID').val(this.warehouseID)
                form.render('select')
                $('#reservoirRemark').val(this.reservoirRemark)
                $('#addName').val(this.addName)
                $('#addTime').val(this.addTime)
                obj.reservoirId = this.reservoirId
                obj.reservoirName = this.reservoirName
                obj.reservoirNumber = this.reservoirNumber
                obj.reservoirArea = this.reservoirArea
                obj.warehouseID = this.warehouseID
                obj.reservoirRemark = this.reservoirRemark
                obj.addName = this.addName
                obj.addTime = this.addTime
            })
        },
    });

    layer.open({
        type: 1,
        title: '编辑库区',
        content: `<form class="layui-form" action="">
                      <div class="layui-form-item">
                        <div class="layui-inline">
                          <label class="layui-form-label">库区名称:</label>
                          <div class="layui-input-inline">
                                <input type="text" name="reservoirNames"lay-verify="required"  id="reservoirNames" autocomplete="off" class="layui-input">
                          </div>
                        </div>
                        <div class="layui-inline">
                          <label class="layui-form-label">库区编号:</label>
                          <div class="layui-input-block">
                            <input type="text" name="reservoirNumber" lay-verify="required" id="reservoirNumber" autocomplete="off" class="layui-input">
                          </div>
                        </div>
                        <div class="layui-inline">
                          <label class="layui-form-label">面积:</label>
                          <div class="layui-input-inline">
                                <input type="text" name="reservoirArea"lay-verify="required"  id="reservoirArea" autocomplete="off" class="layui-input">
                          </div>
                        </div>
                        <div class="layui-inline">
                          <label class="layui-form-label">所属仓库:</label>
                          <div class="layui-col-md6">
                            <select id="warehouseID" name="warehouseID" lay-verify="required" lay-filter="mySelectFilter" class="dialog-select">
                                  <option value="">请选择</option>
                                  <option value="仓库一">仓库一</option>
                                  <option value="仓库二">仓库二</option>
                                  <option value="仓库三">仓库三</option>
                            </select>
                          </div>
                        </div>
                      </div>
                      <div class="layui-form-item layui-form-text">
                        <label class="layui-form-label">备注</label>
                        <div class="layui-input-block">
                                  <textarea placeholder="请输入内容" lay-verify="required" name="reservoirRemark" id="reservoirRemark" class="layui-textarea"></textarea>
                        </div>
                      </div>
                      <div class="layui-form-item" style="text-align:center">
                            <button class="layui-btn" lay-submit lay-filter="demo-login">修改</button>
                        <button type="reset" class="layui-btn layui-btn-primary">取消</button>
                      </div>
                    </form>`,
        area: ['700px', '400px'],
        success: function () {
            var obj1 = {
                reservoirId: '',
                reservoirName: '',
                reservoirNumber: '',
                reservoirArea: '',
                warehouseID: '',
                reservoirRemark: '',
                addName: '',
                addTime: ''
            };
            console.log(obj1)
            // 对弹层中的表单进行初始化渲染
            form.render();
            // 表单提交事件
            form.on('submit(demo-login)', function (data) {
                var field = data.field;
                obj1.reservoirId = obj.reservoirId
                obj1.reservoirName = obj.reservoirName
                obj1.reservoirNumber = field.reservoirNumber
                obj1.reservoirArea = field.reservoirArea
                obj1.warehouseID = field.warehouseID
                obj1.reservoirRemark = field.reservoirRemark
                obj1.addName = obj.addName
                obj1.addTime = obj.addTime
                //console.log(obj1)
                //修改库区
                $.ajax({
                    url: '/Warehouses/UpdReservoir',
                    type: 'post',
                    data: { dto:obj1, token: localStorage.getItem("token") },
                    success: function (res) {
                        var res = JSON.parse(res)
                        console.log(field)
                        console.log(res)
                        if (res.data >= 1) {
                            layer.alert("修改成功");
                            setTimeout(function () {
                                location.reload();
                            }, 2000);
                        }
                        else {
                            layer.alert("修改失败");
                        }
                    }
                })
                return false; // 阻止默认 form 跳转
            });
        }
    });
}
//删除库区
window.deleteData = function (id) {
    layer.confirm('确定删除该条数据吗？', function (index) {
        $.ajax({
            url:'/Warehouses/DelReservoir',
            type: 'post',
            data: { data: JSON.stringify(id), token: localStorage.getItem("token") },
            success: function (res) {
                var res = JSON.parse(res)
                if (res.code == 1) {
                    layer.alert("删除成功");
                    Show();  // 重新加载数据，刷新表格显示
                } else {
                    layer.alert("删除失败");

                }
            },
            error: function () {
                layer.msg('删除失败，请稍后重试', {
                    time: 2000,
                    icon: 2
                });
            }
        });
        layer.close(index);
    })
}